package com.manager.plat.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IFilterDao;
import com.manager.plat.dao.IPartnerLowerDao;
import com.manager.plat.vo.FilterMapVo;
import com.manager.plat.vo.FilterVo;
import com.manager.plat.vo.PartnerLowerVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class FilterAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 804402081159382695L;

	private static Logger logger = Logger.getLogger(FilterAction.class);
	
	private IFilterDao filterDao;					//业务过滤数据接口
	private IPartnerLowerDao partnerLowerDao;		//渠道信息数据接口
	private List<FilterMapVo> filterMapVoList;//渠道扣量组合信息列表
	private List<PartnerLowerVo> partnerLowerVoList;//渠道信息列表
	private FilterVo filterVo;						//业务过滤信息VO
	private String actionPath;						//操作成功跳转路径
	
	/**展示所有渠道扣量配置信息*/
	public String getFilterList(){
		List<FilterVo> filterVoList = filterDao.getFilterList();
		if(filterVoList != null && filterVoList.size() > 0){
			Map<String, List<FilterVo>> filterVoListMap = new LinkedHashMap<String, List<FilterVo>>();
			for(FilterVo filterVo : filterVoList){
				int lower_id = filterVo.getLower_id();
				String lower_name = filterVo.getLower_name();
				String province = filterVo.getProvince();
				String key = lower_id + ":" + lower_name + ":" + province;
				
				if(filterVoListMap.get(key) != null){
					filterVoListMap.get(key).add(filterVo);
				}else{
					List<FilterVo> filterVoListTemp = new ArrayList<>();
					filterVoListTemp.add(filterVo);
					filterVoListMap.put(key, filterVoListTemp);
				}
			}
			
			/**遍历Map 把键值放入一个VO里*/
			if(filterVoListMap != null && filterVoListMap.size() > 0){
				filterMapVoList = new ArrayList<FilterMapVo>();
				for (Map.Entry<String, List<FilterVo>> entry : filterVoListMap.entrySet()) {
					String[] keys = entry.getKey().split(":");
					int lower_id = Integer.parseInt(keys[0]);
					String lower_name = keys[1];
					String province = "";
					if(keys.length > 2){
						province = keys[2];
					}
					FilterMapVo filterMapVo = new FilterMapVo();
					filterMapVo.setLower_id(lower_id);
					filterMapVo.setLower_name(lower_name);
					filterMapVo.setProvince(province);
					filterMapVo.setFilterVoList(entry.getValue());
					
					filterMapVoList.add(filterMapVo);
				}
			}
		}else{
			filterMapVoList = null;
		}
		return SUCCESS;
	}
	
	/**新增渠道扣量配置信息*/
	public String addFilter(){
		filterVo = null;
		partnerLowerVoList = partnerLowerDao.getPartnerLowerList();
		return SUCCESS;
	}
	
	/**保存新增扣量信息*/
	public String saveFilter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		if(filterVo.getLower_id() != 0){
			/**分割所有省份*/
			String[] provinces = filterVo.getProvince().split(":");
			/**根据渠道ID获得渠道下的长号码列表*/
			List<String> sp_number_list = partnerLowerDao.getPartnerLowerSpNumberById(filterVo.getLower_id());
			
			if(sp_number_list != null && sp_number_list.size() > 0){
				List<FilterVo> filterList = new ArrayList<FilterVo>();
				for(String sp_number : sp_number_list){
					if(provinces != null && provinces.length > 0){
						for(String province : provinces){
							FilterVo filter = new FilterVo();
							filter.setLower_id(filterVo.getLower_id());
							filter.setSp_number(sp_number);
							filter.setProvince(province);
							filter.setNum(filterVo.getNum());
							filter.setStart_time(filterVo.getStart_time());
							filter.setEnd_time(filterVo.getEnd_time());
							
							filterList.add(filter);
						}
					}
				}
				
				if(filterList != null && filterList.size() > 0){
					/**批量保存渠道扣量信息*/
					int count = filterDao.batchInsertFilter(filterList);
					if(count > 0){
						logger.info(String.format("[%s]新增[%s]渠道扣量信息成功", userInfoVo.getLogin_username(),filterVo.getProvince()));
					}else{
						logger.info(String.format("[%s]新增[%s]渠道扣量信息失败", userInfoVo.getLogin_username(),filterVo.getProvince()));
					}
				}
			}
			
		}
		actionPath = "filter/getFilterList.action";
		return SUCCESS;
	}
	
	public String modifyLowerFilter(){
		try {
			String province = new String(filterVo.getProvince().getBytes("ISO-8859-1"),"UTF-8");
			filterVo.setProvince(province);
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("修改渠道扣量信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		if(filterVo.getLower_id() != 0){
			partnerLowerVoList = partnerLowerDao.getPartnerLowerList();
		}
		
		return SUCCESS;
	}
	
	public String updateLowerFilter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		if(filterVo.getLower_id() != 0){
			int count = filterDao.updateLowerFilter(filterVo);
			if(count > 0){
				logger.info(String.format("[%s]更新渠道扣量[%s]管控配置成功", userInfoVo.getLogin_username(),filterVo.getProvince()));
			}else{
				logger.info(String.format("[%s]更新渠道扣量[%s]管控配置失败", userInfoVo.getLogin_username(),filterVo.getProvince()));
			}
		}
		actionPath = "filter/getFilterList.action";
		return SUCCESS;
	}
	
	public String modifyFilter(){
		if(filterVo.getId() != 0){
			filterVo = filterDao.getFilterById(filterVo.getId());
		}
		
		partnerLowerVoList = partnerLowerDao.getPartnerLowerList();
		
		return SUCCESS;
	}
	
	public String updateFilter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		if(filterVo.getId() != 0){
			int count  = filterDao.updateFilter(filterVo);
			if(count > 0){
				logger.info(String.format("[%s]更新长号码扣量[%s]配置成功", userInfoVo.getLogin_username(),filterVo.getSp_number()));
			}else{
				logger.info(String.format("[%s]更新长号码扣量[%s]配置失败", userInfoVo.getLogin_username(),filterVo.getSp_number()));
			}
		}
		
		actionPath = "filter/getFilterList.action";
		return SUCCESS;
	}
	
	public String deleteFilter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		
		try {
			filterVo.setProvince(URLDecoder.decode(filterVo.getProvince(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("删除长号码扣量信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		int count = filterDao.deleteFilter(filterVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除长号码[%s][%s]省扣量信息成功", platUser.getLogin_username(),filterVo.getSp_number(),filterVo.getProvince()));
		}else{
			logger.info(String.format("[%s]删除长号码[%s][%s]省扣量信息失败", platUser.getLogin_username(),filterVo.getSp_number(),filterVo.getProvince()));
		}
		
		actionPath = "filter/getFilterList.action";
		return SUCCESS;
	}
	
	public String deleteLowerFilter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		
		try {
			filterVo.setProvince(URLDecoder.decode(filterVo.getProvince(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("删除长号码扣量信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		int count = filterDao.deleteLowerFilter(filterVo.getLower_id(), filterVo.getProvince());
		if(count > 0){
			logger.info(String.format("[%s]删除渠道[%s]扣量信息成功", platUser.getLogin_username(),filterVo.getProvince()));
		}else{
			logger.info(String.format("[%s]删除渠道[%s]扣量信息失败", platUser.getLogin_username(),filterVo.getProvince()));
		}
		
		actionPath = "filter/getFilterList.action";
		return SUCCESS;
	}

	public IFilterDao getFilterDao() {
		return filterDao;
	}

	public void setFilterDao(IFilterDao filterDao) {
		this.filterDao = filterDao;
	}

	public IPartnerLowerDao getPartnerLowerDao() {
		return partnerLowerDao;
	}

	public void setPartnerLowerDao(IPartnerLowerDao partnerLowerDao) {
		this.partnerLowerDao = partnerLowerDao;
	}

	public List<FilterMapVo> getFilterMapVoList() {
		return filterMapVoList;
	}

	public void setFilterMapVoList(List<FilterMapVo> filterMapVoList) {
		this.filterMapVoList = filterMapVoList;
	}

	public List<PartnerLowerVo> getPartnerLowerVoList() {
		return partnerLowerVoList;
	}

	public void setPartnerLowerVoList(List<PartnerLowerVo> partnerLowerVoList) {
		this.partnerLowerVoList = partnerLowerVoList;
	}

	public FilterVo getFilterVo() {
		return filterVo;
	}

	public void setFilterVo(FilterVo filterVo) {
		this.filterVo = filterVo;
	}

	public String getActionPath() {
		return actionPath;
	}

	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}
